package com.tripspins.app.helper;

import android.content.Context;
import android.net.Uri;
import android.os.Environment;

import com.tripspins.app.R;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by João.Rutkoski on 25/06/13.
 */
public class FileHelper {

    private Context appContext;
    private final String PICTURE_EXTENSION = ".jpg";
    private final String VIDEO_EXTENSION = ".mp4";
    private final String AUDIO_EXTENSION = ".3gp";
    private final String TEXT_EXTENSION = ".txt";



    public FileHelper(Context context)
    {
        appContext = context;
    }

    public void persistTempFiles(long tripId, long pinId)
    {
        File tempDir = getAttachmentsTemporaryDirectory();
        File tempThumbnails = getThumbnailsTemporaryDirectory();

        File attachmentDir = getAttachmentsDirectory(tripId,pinId);
        File thumbnailsDir = getThumbnailsDirectory(tripId, pinId);

        thumbnailsDir.mkdirs();
        attachmentDir.mkdirs();

        for(File file : tempDir.listFiles())
        {
//            ThumbnailsHelper.generateThumbnail(file);
            file.renameTo( new File(attachmentDir.getPath() + "/" + file.getName()) );
        }
        for(File file : tempThumbnails.listFiles())
        {
            file.renameTo( new File(thumbnailsDir.getPath() + "/" + file.getName()) );
        }

    }

    public Uri saveFile(FILE_TYPE fileType)
    {
        File file = null;
        File storageDir = getAttachmentsTemporaryDirectory();
        storageDir.mkdirs();
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        switch (fileType) {
            case IMAGE:
                file = new File(storageDir + File.separator + "IMG_"+ timeStamp + PICTURE_EXTENSION);
                break;
            case VIDEO:
                file = new File(storageDir + File.separator + "VID_"+ timeStamp + VIDEO_EXTENSION);
                break;
            case AUDIO:
                file = new File(storageDir + File.separator + "AUD_"+ timeStamp + AUDIO_EXTENSION);
                break;
        }
        return  Uri.fromFile(file);
    }

    public File saveWeatherIcon()
    {
        File file = null;
        File storageDir = getAttachmentsTemporaryDirectory();
        storageDir.mkdirs();
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        file = new File(storageDir + File.separator + "WEATHER_ICON" + PICTURE_EXTENSION);
        return  file;
    }


    public File getAttachmentsTemporaryDirectory()
    {
        String path = Environment.getExternalStorageDirectory().getPath() + "/" +
                appContext.getString(R.string.app_name) + "/TEMP/ATTACHMENTS/";

        File storageDir = new File(path );
        storageDir.mkdirs();
        return storageDir;
    }
    public File getThumbnailsTemporaryDirectory()
    {
        String path = Environment.getExternalStorageDirectory().getPath() + "/" +
                appContext.getString(R.string.app_name) + "/TEMP/THUMBNAILS/";

        File storageDir = new File(path );
        storageDir.mkdirs();
        return storageDir;
    }

    public File getAttachmentsDirectory(long tripId, long pinId)
    {
        String path = Environment.getExternalStorageDirectory().getPath() + "/" +
                     appContext.getString(R.string.app_name) + "/" +
                     tripId + "/" +
                     pinId  + "/ATTACHMENTS/";

        File storageDir = new File(path );
        storageDir.mkdirs();
        return storageDir;
    }

    public File getThumbnailsDirectory(long tripId, long pinId)
    {
        String path = Environment.getExternalStorageDirectory().getPath() + "/" +
                appContext.getString(R.string.app_name) + "/" +
                tripId + "/" +
                pinId  + "/THUMBNAILS/";

        File storageDir = new File(path );
        storageDir.mkdirs();
        return storageDir;
    }
    public File getMapThumbnail(long tripId)
    {
        String path = Environment.getExternalStorageDirectory().getPath() + "/" +
                appContext.getString(R.string.app_name) + "/" +
                tripId + "/";

        File mapDir = new File(path);
        mapDir.mkdirs();
        File mapFile = new File(mapDir.getPath() + "/map.png");

        return mapFile;
    }

    public void moveFile(String pathFrom, String pathTo)
    {
            File from = new File(pathFrom);
            from.renameTo(new File(pathTo));
    }

    public void cleanTempFolders()
    {
        File tempFolder = getAttachmentsTemporaryDirectory();
        for (File f : tempFolder.listFiles())
        {
            f.delete();
        }
        tempFolder = getThumbnailsTemporaryDirectory();
        for (File f : tempFolder.listFiles())
        {
            f.delete();
        }
    }


    public Uri saveFile(FILE_TYPE image, String data) {

        File srcImage = new File(data);
        File dstImage = new File(saveFile(image).getPath());
        try {
            tryCopy(srcImage, dstImage );

        } catch (IOException e) {
            e.printStackTrace();
        }

        return Uri.fromFile(dstImage);
    }
    public void tryCopy(File src, File dst) throws IOException {
        InputStream in = new FileInputStream(src);
        OutputStream out = new FileOutputStream(dst);

        // Transfer bytes from in to out
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    }
}
